<?php
namespace App\Http\Controllers\ApiAdmin;

use App\Service\Admin\AdminService;
use Illuminate\Http\Request;

class AdminController extends BaseController
{
    //管理员

    public function __construct(AdminService $service)
    {
        $this->db = $service;
    }

    //列表
    public function getIndex(Request $request)
    {
        $token = $request->post('token', '');
        $res = $this->db->getIndex($token);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //详情
    public function getOne(Request $request)
    {
        $id = $request->post('id', 0);
        $res = $this->db->getOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //修改
    public function modify(Request $request)
    {
        $id = $request->post('id', 0);
        $title = $request->post('title', '');
        if (!$title) {
            return $this->toJson(-2, '手机号必须的');
        }
        if ($id) {
            $res = $this->db->modify($id, $title);
        } else {
            $res = $this->db->add($title);
        }
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //修改
    public function setPwd(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['pwdold'] = $request->post('pwdold', '');
        $pas['pwdnew'] = $request->post('pwdnew', '');
        if (!$id) {
            return $this->toJson(-2, 'ID必须的');
        }
        $res = $this->db->setPwd($id, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function del(Request $request)
    {
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, 'ID必须的');
        }
        $res = $this->db->del($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //禁用启用
    public function able(Request $request)
    {
        $id = $request->post('id', 0);
        $able = $request->post('able', 0); //0正常1禁用
        if (!$id) {
            return $this->toJson(-2, 'ID必须的');
        }
        $res = $this->db->able($id, $able);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}